package cn.kent.base.sleep;

/**
 * 验证 正在睡眠的线程被 interrupt 是否会sleep处抛出异常。
 * 结果：抛出异常： java.lang.InterruptedException: sleep interrupted （睡眠被中断）
 *
 * sleep方法不能传入负数： Exception in thread "main" java.lang.IllegalArgumentException: timeout value is negative
 */
public class SleepTest2 {

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("child thread begin sleep");
                    Thread.sleep(10000);
                    System.out.println("child thread wake up.");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();
        Thread.sleep(2000); // 主线程休眠2s
        thread.interrupt(); // 主线程中断子线程
    }
}
